$(function(){
	$('#anime').datagrid({
		onLoadSuccess:function(data){
			//alert(JSON.stringify(data));
		},
		onLoadError:function(){
		},
		onClickRow:freshEpisode
	});
	$('#episode').edatagrid({
		url:'episodeManage/getEpisode',
		saveUrl: 'episodeManage/saveEpisode',  
        updateUrl: 'episodeManage/updateEpisode',  
        destroyUrl: 'episodeManage/deleteEpisode',
        //客户端分页
        //loadFilter:pagerFilter,
        //新增前初始化新增数据
        onBeforeEdit:function(index, row){
        	if($('#anime').datagrid('getSelected')!=null)
    		{
        		if(row.isNewRecord==true)
        		{
	        		row.anime_id = $('#anime').datagrid('getSelected').id;
	        		row.date = new Date().format("yyyy-MM-dd hh:mm:ss");
        		}
    		}
        	else
        	{
        		return false;
        	}
        }
	});
	$('#animeSearch').live('click',function(){
		$('#anime').datagrid('load',{'code':$('#animeCode').val(),'name':$('#animeName').val()});
	});
	$('#addEpisode').live('click',function(){
		if($('#anime').datagrid('getSelected')!=null)
		{
			$('#episode').edatagrid('addRow');
		}
		else
		{
			$.messager.show({  
                msg:'必须先选择一个动漫！',
                timeout:2000,
                showType:'show'  
            });
		}
	});
	$('#deleteEpisode').live('click',function(){
		$('#episode').edatagrid('destroyRow');
	});
	$('#saveEpisode').live('click',function(){
		$('#episode').edatagrid('saveRow');
	});
	$('#cancelEpisode').live('click',function(){
		$('#episode').edatagrid('cancelRow');
	});
	$('#addAnime').live('click',function(){
		window.open('animeManage/getAnime', '动漫编辑', openWindowConfig());
	});
	$('#editAnime').live('click',function(){
		var anime = $('#anime').datagrid('getSelected');
		if(anime==null)
		{
			$.messager.show({  
                msg:'必须先选择一个动漫！',
                timeout:2000,
                showType:'show'  
            });
		}else
		{
			window.open('animeManage/getAnime?animeId='+anime.id, '动漫编辑', openWindowConfig());
		}
	});
	$('#deleteAnime').live('click',function(){
		var animeId = $('#anime').datagrid('getSelected').id;
		$.ajax({
			url:'animeManage/deleteAnime',
			dataType:'json',
			data:{'animeId':animeId},
			success:function(){
				$.messager.show({  
	                msg:'删除成功！',
	                timeout:2000,
	                showType:'show'  
	            });
				animeReload();
			},
			error:function (XMLHttpRequest, textStatus, errorThrown){
				$.messager.show({  
	                msg:textStatus,
	                timeout:2000,
	                showType:'show'  
	            });
			}
			
		});
		
	});
	$('#cvEdit').live('click',function(){
		window.open('cvManage', '声优编辑', openWindowConfig());
	});
	$('#typeEdit').live('click',function(){
		window.open('typeManage', '类型编辑', openWindowConfig());
	});
	$("#episodeImport").uploadify({
		buttonText:'导入剧集',
		height: 20,
		swf: 'uploadify/uploadify.swf',
		uploader: 'importEpisode',
		width: 60,
		fileSizeLimit: '10MB',
		fileTypeExts: '*.xls; *.xlsx;',
		onUploadSuccess: uploadSuccess,
		onUploadStart : function(file) 
        {
            $("#episodeImport").uploadify('settings','formData',{'animeId':$('#anime').datagrid('getSelected').id});
        },
        onSWFReady :function()
        {
    		$("#episodeImport").uploadify('disable',true);
        }
	});
});
function uploadSuccess(file, data, response)
{
	if(response)
	{
		$.messager.show({  
	        msg:'导入成功！',
	        timeout:2000,
	        showType:'show'  
	    });
		$('#episode').edatagrid('reload');
	}else
	{
		$.messager.show({  
	        msg:'导入失败！',
	        timeout:2000,
	        showType:'show'  
	    });
	}
}
function freshEpisode(rowIndex, rowData)
{
	$('#episode').edatagrid('load',{'animeId':rowData.id});
	$("#episodeImport").uploadify('disable',false);
}
function openWindowConfig()
{
	var w = 900;
	var h = 670;
	var l = (screen.width - w) / 2; 
    var t = (screen.height - h) / 2; 
    var s = 'width=' + w + ', height=' + h + ', top=' + t + ', left=' + l; 
    s += ', toolbar=no, menubar=no, location=no';
    return s;
}
function animeReload()
{
	$('#anime').datagrid('reload');
}